﻿using ConfExpert.Datasets;
using ConfExpert.Utility;
using DevExpress.Utils;
using DevTeam;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo
{
    public partial class Kodlar : XPopupForm
    {
        int ProductId;
        public Kodlar(int _ProductId)
        {
            InitializeComponent();
            ProductId = _ProductId;
        }

        public Kodlar(int _ProductId, bool dummy)
            : this(_ProductId)
        {
            ExpandCollapse(sccKodlar, AcKapa.Collapse);
            btnKaydet.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
        }

        public int VendorId;
        public string UreticiKodu;
        BindingSource bsUK, bsMK, bsProduct;
        private void Kodlar_Load(object sender, EventArgs e)
        {

            TAM.UreticiKodlariTableAdapter = new Datasets.MyDataTableAdapters.UreticiKodlariTableAdapter();
            TAM.MusteriKodlariTableAdapter = new Datasets.MyDataTableAdapters.MusteriKodlariTableAdapter();

            System.Data.DataRow drKumas = AppSession.Lookups.V_Kumas.FindByProductId(ProductId);

            if (drKumas != null)
                txtInfo.Caption = drKumas["Name"].ToStr();
            else
            {
                System.Data.DataRow drTela = AppSession.Lookups.Tela.FindByProductId(ProductId);
                if (drTela != null)
                    txtInfo.Caption = drTela["Name"].ToStr();
            }

            bsProduct = new BindingSource(ds, "Product");
            bsUK = new BindingSource(bsProduct, "FK_UreticiKodlari_Product_ProductId");
            bsMK = new BindingSource(bsProduct, "FK_MusteriKodlari_Product_ProductId");
            AddBsToList(bsUK, bsMK);

            //TODO: bu kısmın güncellenmesi gerek. ((RepositoryItemPictureEdit)pictureEdit1.Edit).DataBindings.Add("EditValue", bsProduct, "PhotoMikro");
            pictureEdit1.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            InitGridView(gvUK, bsUK);
            gvUK.Columns["VendorId"].ColumnEdit = RI(Rp.V_VendorK);

            gvUK.YanYanaGetirSagina("UreticiKodu", "VendorId");
            gvUK.MakeColumnInvisible("UreticiKodlariId", "Scan", "ProductId");
            gvUK.MakeEditable();
            gvUK.EnableNewRow();


            gvUK.RowCellStyle += gvUK_RowCellStyle;

            InitGridView(gvMK, bsMK);

            gvMK.MakeColumnInvisible("MusteriKodlariId", "Scan", "ProductId");
            gvMK.MakeEditable();
            gvMK.EnableNewRow();
            gvMK.RowCellStyle += gvMK_RowCellStyle;

            btnKaydet.ItemClick += btnKaydet_ItemClick;
            sccKodlar.AutoSize();

            GetDetails("Product;UreticiKodlari;MusteriKodlari", "GetUMKodlar", ProductId);

            //((RepositoryItemGridLookUpEdit)gvMK.Columns["CustomerId"].ColumnEdit).DoubleClick += new EventHandler(Kodlar_DoubleClick);
            //gvMK.DoubleClick += new EventHandler(gvMK_DoubleClick);
            if (btnKaydet.Visibility == DevExpress.XtraBars.BarItemVisibility.Never)
                gvUK.DoubleClick += new EventHandler(gvUK_DoubleClick);

            ds.MusteriKodlari.TableNewRow += MusteriKodlari_TableNewRow;
            ds.UreticiKodlari.TableNewRow += MusteriKodlari_TableNewRow;

            gvMK.MakeColumnsReadOnly("UserId");
            gvUK.MakeColumnsReadOnly("UserId");

            gvMK.BestFitColumns();
            gvUK.BestFitColumns();
        }

        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Kaydet();
        }

        void MusteriKodlari_TableNewRow(object sender, System.Data.DataTableNewRowEventArgs e)
        {
            e.Row["UserId"] = WhoAmI;
        }

        void gvUK_DoubleClick(object sender, EventArgs e)
        {
            VendorId = (int)(gvUK.GetFocusedRowCellValue("VendorId"));
            UreticiKodu = gvUK.GetFocusedRowCellValue("UreticiKodu").ToString();

            Kaydet();
        }

        void gvMK_DoubleClick(object sender, EventArgs e)
        {
            using (Araclar.Brand2Customer b = new Araclar.Brand2Customer())
            {
                if (b.ShowDialog() == DialogResult.OK)
                {
                    if (gvMK.FocusedRowHandle != DevExpress.XtraGrid.GridControl.NewItemRowHandle)
                        gvMK.SetFocusedRowCellValue("CustomerId", b.CustomerId);
                    else
                    {
                        MyData.MusteriKodlariRow mkr = ds.MusteriKodlari.NewMusteriKodlariRow();
                        mkr.ProductId = ProductId;
                        mkr.CustomerId = b.CustomerId;
                        ds.MusteriKodlari.AddMusteriKodlariRow(mkr);
                    }

                    gvMK.BestFitColumns();
                }
            }
        }

        private void Kaydet()
        {
            for (int i = ds.UreticiKodlari.Rows.Count - 1; i >= 0; i--)
                if (ds.UreticiKodlari[i].IsVendorIdNull())
                    ds.UreticiKodlari.RemoveUreticiKodlariRow(ds.UreticiKodlari[i]);

            for (int i = ds.MusteriKodlari.Rows.Count - 1; i >= 0; i--)
                if (ds.MusteriKodlari[i].IsCustomerIdNull())
                    ds.MusteriKodlari.RemoveMusteriKodlariRow(ds.MusteriKodlari[i]);

            Save();
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        void gvUK_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.RowHandle > -1)
                if (e.Column.FieldName.Contains("VendorId"))
                    e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);
        }

        void gvMK_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.RowHandle > -1)
                if (e.Column.FieldName.Contains("BrandId"))
                    e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);
        }


    }
}